﻿Partial Public Class Main

    Private Sub MemoryData()
        
        PageLifeExpectancy()
        FreeListStalls()
        LazyWrites()
        CheckPointPages()
        PageLookups()
        PageReads()
        PageWrites()
        ReadAheads()

    End Sub

    Private Sub PageLifeExpectancy()
        CalculateCounter("Page life expectancy",
                         CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                         Me.txtPLEMin,
                         Me.txtPLEMax,
                         Me.txtPLEAvg,
                         Me.SparkLinePageLifeExpectancy)


        GetThresholds("SQLServer:Buffer Manager", _
                      "Page life expectancy", _
                      Me.txtPLEMin, _
                      Me.txtPLEMax, _
                      Me.txtPLEAvg)

    End Sub

    Private Sub FreeListStalls()
        CalculateCounter("Free list stalls/sec",
                         CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                         Me.txtFLSMin,
                         Me.txtFLSMax,
                         Me.txtFLSAvg,
                         Me.SparkLineFreeListStalls)

        GetThresholds("SQLServer:Buffer Manager", _
                      "Free list stalls/sec", _
                      Me.txtFLSMin, _
                      Me.txtFLSMax, _
                      Me.txtFLSAvg)

    End Sub

    Private Sub LazyWrites()

        CalculateCounter("Lazy writes/sec",
                         CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                         Me.txtLWMin,
                         Me.txtLWMax,
                         Me.txtLWAvg,
                         Me.SparkLineLazyWrites)

        GetThresholds("SQLServer:Buffer Manager", _
                      "Lazy writes/sec", _
                      Me.txtLWMin, _
                      Me.txtLWMax, _
                      Me.txtLWAvg)

    End Sub

    Private Sub CheckPointPages()

        CalculateCounter("Checkpoint pages/sec",
                         CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                         Me.txtCHKPMin,
                         Me.txtCHKPMax,
                         Me.txtCHKPAvg,
                         Me.SparklineCheckPoints)

        GetThresholds("SQLServer:Buffer Manager", _
                      "Checkpoint pages/sec", _
                      Me.txtCHKPMin, _
                      Me.txtCHKPMax, _
                      Me.txtCHKPAvg)

    End Sub

    Private Sub PageLookups()

        CalculateCounter("Page lookups/sec",
                         CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                         Me.txtPLMin,
                         Me.txtPLMax,
                         Me.txtPLAvg,
                         Me.SparklinePageLookups)

        GetThresholds("SQLServer:Buffer Manager", _
                     "Page lookups/sec", _
                     Me.txtPLMin, _
                     Me.txtPLMax, _
                     Me.txtPLAvg)
    End Sub

    Private Sub PageReads()

        CalculateCounter("Page reads/sec",
                        CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                        Me.txtPRmin,
                        Me.txtPRmax,
                        Me.txtPRAvg,
                        Me.SparklinePageReads)

        'TODO: Get from threshold
        'Color text for warning....
        If Me.txtPRAvg.Text <> String.Empty Then
            If CDbl(Me.txtPRAvg.Text) > 90 Then
                Me.txtPRAvg.BackColor = Color.Red
            Else : Me.txtPRAvg.BackColor = Color.LawnGreen
            End If
            If CDbl(Me.txtPRmax.Text) > 90 Then
                Me.txtPRmax.BackColor = Color.Red
            Else : Me.txtPRmax.BackColor = Color.LawnGreen
            End If
            If CDbl(Me.txtPRmin.Text) > 90 Then
                Me.txtPRmin.BackColor = Color.Red
            Else : Me.txtPRmin.BackColor = Color.LawnGreen
            End If
        End If

    End Sub

    Private Sub PageWrites()

        CalculateCounter("Page writes/sec",
                        CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                        Me.txtPWMin,
                        Me.txtPWMax,
                        Me.txtPWAvg,
                        Me.SparklinePageWrites)

        GetThresholds("SQLServer:Buffer Manager", _
                     "Page writes/sec", _
                     Me.txtPWMin, _
                     Me.txtPWMax, _
                     Me.txtPWAvg)

    End Sub

    Private Sub ReadAheads()

        CalculateCounter("Readahead pages/sec",
                CStr(IIf(instanceName = "Default", "SQLServer:Buffer Manager", "MSSQL$" + instanceName + ":Buffer Manager")),
                Me.txtRARMin,
                Me.txtRARMax,
                Me.txtRARAvg,
                Me.SparklineReadAheadReads)

        'Color text for warning....
        GetThresholds("SQLServer:Buffer Manager", _
                     "Readahead pages/sec", _
                     Me.txtRARMin, _
                     Me.txtRARMax, _
                     Me.txtRARAvg)

    End Sub
End Class
